package io.lkt.modules.sys.controller;

import io.lkt.common.util.Query;
import io.lkt.common.utils.DateUtil;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.modules.sys.dao.SysUserGzhDao;
import io.lkt.modules.sys.entity.*;
import io.lkt.modules.sys.service.BBrandInfoService;
import io.lkt.modules.sys.service.GzhRankService;
import io.lkt.modules.sys.service.GzhSummaryLogService;
import io.lkt.modules.sys.service.SysUserRoleService;
import io.lkt.modules.sys.shiro.WeixinService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/sys/gzhrank")
public class GzhRankController extends AbstractController {
    @Autowired
    private GzhRankService gzhRankService;

    @Autowired
    private SysUserGzhDao sysUserGzhDao;

    @Autowired
    private GzhSummaryLogService gzhSummaryLogService;

    @Autowired
    private SysUserRoleService sysUserRoleService;
    /**
     * 关注排行榜
     */
    @RequestMapping("/rankJRGZYHS")
    public R rankJRGZYHS(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhRankEntity> list = gzhRankService.rankJRGZYHS(params);

        // 需要筛选的条件：从 list 中筛选出当前用户公众号权限
        List<String> gzhCodeList = this.sysUserRoleService.queryGzhCodeList(getUserId());
        list = list.stream().filter(GzhRankEntity -> gzhCodeList.contains(GzhRankEntity.getGzhCode())).collect(Collectors.toList());

        ArrayList category = new ArrayList();
        ArrayList bar = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            category.add(list.get(i).getGzhName());
            bar.add(list.get(i).getCount());
        }

        return R.ok().put("category", category).put("bar", bar);
    }


    /**
     * 文章点击排行榜
     */
    @RequestMapping("/rankWZDJS")
    public R rankWZDJS(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhRankEntity> list = gzhRankService.rankWZDJS(params);

        // 需要筛选的条件：从 list 中筛选出当前用户公众号权限
        List<String> gzhCodeList = this.sysUserRoleService.queryGzhCodeList(getUserId());
        list = list.stream().filter(GzhRankEntity -> gzhCodeList.contains(GzhRankEntity.getGzhCode())).collect(Collectors.toList());

        ArrayList category = new ArrayList();
        ArrayList bar0 = new ArrayList();
        ArrayList bar1 = new ArrayList();
        ArrayList bar2 = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            category.add(list.get(i).getGzhName());
            bar0.add(list.get(i).getCount());
            bar1.add(list.get(i).getDisCount());
            bar2.add(list.get(i).getVipCount());
        }

        return R.ok().put("category", category)
                .put("bar0", bar0)
                .put("bar1", bar1)
                .put("bar2", bar2);
    }


    /**
     * 文章分享排行榜
     */
    @RequestMapping("/rankWZFXS")
    public R rankWZFXS(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhRankEntity> list = gzhRankService.rankWZFXS(params);

        // 需要筛选的条件：从 list 中筛选出当前用户公众号权限
        List<String> gzhCodeList = this.sysUserRoleService.queryGzhCodeList(getUserId());
        list = list.stream().filter(GzhRankEntity -> gzhCodeList.contains(GzhRankEntity.getGzhCode())).collect(Collectors.toList());

        ArrayList category = new ArrayList();
        ArrayList bar0 = new ArrayList();
        ArrayList bar1 = new ArrayList();
        ArrayList bar2 = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            category.add(list.get(i).getGzhName());
            bar0.add(list.get(i).getCount());
            bar1.add(list.get(i).getDisCount());
            bar2.add(list.get(i).getVipCount());
        }

        return R.ok().put("category", category)
                .put("bar0", bar0)
                .put("bar1", bar1)
                .put("bar2", bar2);
    }


    /**
     * 关键词回复排行榜
     */
    @RequestMapping("/rankWORKS")
    public R rankWORKS(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhRankEntity> list = gzhRankService.rankWORKS(params);

        // 需要筛选的条件：从 list 中筛选出当前用户公众号权限
        List<String> gzhCodeList = this.sysUserRoleService.queryGzhCodeList(getUserId());
        list = list.stream().filter(GzhRankEntity -> gzhCodeList.contains(GzhRankEntity.getGzhCode())).collect(Collectors.toList());

        ArrayList category = new ArrayList();
        ArrayList bar0 = new ArrayList();
        ArrayList bar1 = new ArrayList();
        ArrayList bar2 = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            category.add(list.get(i).getGzhName());
            bar0.add(list.get(i).getCount());
            bar1.add(list.get(i).getDisCount());
            bar2.add(list.get(i).getVipCount());
        }

        return R.ok().put("category", category)
                .put("bar0", bar0)
                .put("bar1", bar1)
                .put("bar2", bar2);
    }


    /**
     * 打卡排行榜
     */
    @RequestMapping("/rankCLOCK")
    public R rankCLOCK(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhRankEntity> list = gzhRankService.rankCLOCK(params);

        // 需要筛选的条件：从 list 中筛选出当前用户公众号权限
        List<String> gzhCodeList = this.sysUserRoleService.queryGzhCodeList(getUserId());
        list = list.stream().filter(GzhRankEntity -> gzhCodeList.contains(GzhRankEntity.getGzhCode())).collect(Collectors.toList());

        ArrayList category = new ArrayList();
        ArrayList bar0 = new ArrayList();
        ArrayList bar1 = new ArrayList();
        ArrayList bar2 = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            category.add(list.get(i).getGzhName());
            bar0.add(list.get(i).getCount());
            bar1.add(list.get(i).getDisCount());
            bar2.add(list.get(i).getVipCount());
        }

        return R.ok().put("category", category)
                .put("bar0", bar0)
                .put("bar1", bar1)
                .put("bar2", bar2);
    }


    /**
     * 会员排行榜
     */
    @RequestMapping("/rankPay")
    public R rankPay(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhRankEntity> list = gzhRankService.rankPay(params);

        // 需要筛选的条件：从 list 中筛选出当前用户公众号权限
        List<String> gzhCodeList = this.sysUserRoleService.queryGzhCodeList(getUserId());
        list = list.stream().filter(GzhRankEntity -> gzhCodeList.contains(GzhRankEntity.getGzhCode())).collect(Collectors.toList());

        ArrayList category = new ArrayList();
        ArrayList bar0 = new ArrayList();
        ArrayList bar1 = new ArrayList();
        ArrayList bar2 = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            category.add(list.get(i).getGzhName());
            bar0.add(list.get(i).getVipCount());
            bar1.add(list.get(i).getDisCount());
            bar2.add(list.get(i).getCount());
        }

        return R.ok().put("category", category)
                .put("bar0", bar0)
                .put("bar1", bar1)
                .put("bar2", bar2);
    }


    /**
     * 推送日期对比统计
     */
    @RequestMapping("/pushComparisonStatistics")
    public R pushComparisonStatistics(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {

            Calendar cal = Calendar.getInstance();
            cal.setTime(DateUtil.getCurDate());//设置起时间
            cal.add(Calendar.DATE, -6);//减6天

            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", cal.getTime()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<PushComparisonStatisticsEntity> list = gzhRankService.pushComparisonStatistics(params);

        ArrayList pushDate = new ArrayList();
        ArrayList bar6 = new ArrayList();
        ArrayList bar8 = new ArrayList();
        ArrayList baryh = new ArrayList();
        ArrayList bar11 = new ArrayList();
        ArrayList bar18 = new ArrayList();
        ArrayList bar20 = new ArrayList();
        ArrayList bar21 = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            pushDate.add(list.get(i).getPushDate());
            bar6.add(list.get(i).getPushT6());
            bar8.add(list.get(i).getPushT8());
            baryh.add(list.get(i).getPushTyh());
            bar11.add(list.get(i).getPushT11());
            bar18.add(list.get(i).getPushT18());
            bar20.add(list.get(i).getPushT20());
            bar21.add(list.get(i).getPushT21());
        }

        return R.ok().put("pushDate", pushDate)
                .put("bar6", bar6)
                .put("bar8", bar8)
                .put("baryh", baryh)
                .put("bar11", bar11)
                .put("bar18", bar18)
                .put("bar20", bar20)
                .put("bar21", bar21);
    }


    /**
     * 关键词回复日期对比统计
     */
    @RequestMapping("/keywordsReplyStatistics")
    public R keywordsReplyStatistics(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(DateUtil.getCurDate());//设置起时间
            cal.add(Calendar.DATE, -6);//减6天

            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", cal.getTime()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhRankEntity> list = gzhRankService.keywordsReplyStatistics(params);

        ArrayList attackTime = new ArrayList();
        ArrayList bar0 = new ArrayList();
        ArrayList bar1 = new ArrayList();
        ArrayList bar2 = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            attackTime.add(list.get(i).getAttackTime());
            bar0.add(list.get(i).getVipCount());
            bar1.add(list.get(i).getDisCount());
            bar2.add(list.get(i).getCount());
        }

        return R.ok().put("category", attackTime)
                .put("bar0", bar0)
                .put("bar1", bar1)
                .put("bar2", bar2);
    }


    /**
     * 订单日期对比统计
     */
    @RequestMapping("/orderPayStatistics")
    public R orderPayStatistics(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(DateUtil.getCurDate());//设置起时间
            cal.add(Calendar.DATE, -6);//减6天

            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", cal.getTime()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhRankEntity> list = gzhRankService.orderPayStatistics(params);

        ArrayList attackTime = new ArrayList();
        ArrayList bar0 = new ArrayList();
        ArrayList bar1 = new ArrayList();
        ArrayList bar2 = new ArrayList();
        ArrayList bar3 = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            attackTime.add(list.get(i).getAttackTime());
            bar0.add(list.get(i).getCount());
            bar1.add(list.get(i).getDisCount());
            bar2.add(list.get(i).getVipCount());
            bar3.add(list.get(i).getTotalAmount());
        }

        return R.ok().put("category", attackTime)
                .put("bar0", bar0)
                .put("bar1", bar1)
                .put("bar2", bar2)
                .put("bar3", bar3);
    }


    /**
     * 文章阅读日期对比统计
     */
    @RequestMapping("/articleLookStatistics")
    public R articleLookStatistics(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(DateUtil.getCurDate());//设置起时间
            cal.add(Calendar.DATE, -6);//减6天

            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", cal.getTime()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<PushComparisonStatisticsEntity> list = gzhRankService.articleLookStatistics(params);

        ArrayList lookDate = new ArrayList();
        ArrayList lookCount = new ArrayList();
        ArrayList shareCount = new ArrayList();
        ArrayList lookUserCount = new ArrayList();
        ArrayList shareUserCount = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            lookDate.add(list.get(i).getPushDate());
            lookCount.add(list.get(i).getPushT6());
            shareCount.add(list.get(i).getPushT8());
            lookUserCount.add(list.get(i).getPushT11());
            shareUserCount.add(list.get(i).getPushT18());
        }

        return R.ok().put("lookDate", lookDate)
                .put("lookCount", lookCount)
                .put("shareCount", shareCount)
                .put("lookUserCount", lookUserCount)
                .put("shareUserCount", shareUserCount);
    }


    /**
     * 公众号粉丝日期对比统计
     */
    @RequestMapping("/fansStatistics")
    public R fansStatistics(@RequestBody Map<String, Object> params) {

        if (params.get("startDate") != null && !params.get("startDate").toString().trim().equals("")) {
            params.put("startDate", params.get("startDate"));
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(DateUtil.getCurDate());//设置起时间
            cal.add(Calendar.DATE, -6);//减6天

            params.put("startDate", DateUtil.getFormatDate("yyyy-MM-dd", cal.getTime()) + " 00:00:00");
        }


        if (params.get("endDate") != null && !params.get("endDate").toString().trim().equals("")) {
            params.put("endDate", params.get("endDate") + " 23:59:59");
        } else {
            params.put("endDate", DateUtil.getFormatDate("yyyy-MM-dd", DateUtil.getCurDate()) + " 23:59:59");
        }

        //Query query = new Query(params);

        List<GzhSummaryLogEntity> list = gzhSummaryLogService.fansStatistics(params);

        ArrayList getDate = new ArrayList();
        ArrayList newUserCount = new ArrayList();
        ArrayList cancelUserCount = new ArrayList();
        ArrayList growthUserCount = new ArrayList();
        for (int i = list.size() - 1; i >= 0; i--) {
            getDate.add(list.get(i).getGetDate());
            newUserCount.add(list.get(i).getNewUserCount());
            cancelUserCount.add(list.get(i).getCancelUserCount());
            growthUserCount.add(list.get(i).getGrowthUserCount());
        }

        return R.ok().put("getDate", getDate)
                .put("newUserCount", newUserCount)
                .put("cancelUserCount", cancelUserCount)
                .put("growthUserCount", growthUserCount);
    }


    /**
     * 计算公众号粉丝号
     */
    @RequestMapping("/getFans")
    public R getFans(@RequestBody Map<String, Object> params) throws WxErrorException, ParseException {

        if (!params.get("gzhCode").toString().equals("")) {
            String gzhCode = params.get("gzhCode").toString();
            SysGzhEntity sysGzh = sysUserGzhDao.getGzhByCode(gzhCode);


            String maxDate =gzhSummaryLogService.getMaxDate(gzhCode);

            if(maxDate ==null || (maxDate!=null && maxDate.equals(""))) {
                maxDate="2020-02-29 00:00:00";
            }else {
                maxDate = maxDate + " 00:00:00";
            }

            Calendar calendar = Calendar.getInstance();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date indDate=sdf.parse(maxDate);
            calendar.setTime(indDate);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            indDate= calendar.getTime();

            //calendar.setTime(new Date());
            //calendar.add(Calendar.DAY_OF_MONTH, -1);
            Date endDate= new Date();
            WxMpServiceImpl wxMpService = WeixinService.getWeixinService(sysGzh);
            int betDays= DateUtil.daysBetween(indDate,endDate);
            while (betDays>0) {

                List<WxDataCubeUserSummary> wxDataCubeUserSummaries = wxMpService.getDataCubeService().getUserSummary(indDate, indDate);
                //List<WxDataCubeUserCumulate> wxDataCubeUserCumulates = wxMpService.getDataCubeService().getUserCumulate(newTime1, newTime1);

                GzhSummaryLogEntity userVitalEntity = new GzhSummaryLogEntity();

                Integer gzhxzgzCount = 0;
                Integer cancelUser = 0;

                for (WxDataCubeUserSummary wdcus : wxDataCubeUserSummaries) {

                    if (wdcus.getUserSource().equals(0)) {
                        userVitalEntity.setS0Count(wdcus.getNewUser());
                    } else if (wdcus.getUserSource().equals(1)) {
                        userVitalEntity.setS1Count(wdcus.getNewUser());
                    } else if (wdcus.getUserSource().equals(17)) {
                        userVitalEntity.setS17Count(wdcus.getNewUser());
                    } else if (wdcus.getUserSource().equals(30)) {
                        userVitalEntity.setS30Count(wdcus.getNewUser());
                    }
                    gzhxzgzCount = gzhxzgzCount + wdcus.getNewUser();
                    cancelUser = cancelUser + wdcus.getCancelUser();

                }

                userVitalEntity.setNewUserCount(gzhxzgzCount);
                userVitalEntity.setCancelUserCount(cancelUser);
                userVitalEntity.setGrowthUserCount(gzhxzgzCount - cancelUser);
                userVitalEntity.setGetDate(DateUtil.getFormatDate("yyyy-MM-dd", indDate));
                userVitalEntity.setGzhCode(gzhCode);

                gzhSummaryLogService.addData(userVitalEntity);


                calendar.setTime(indDate);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                indDate= calendar.getTime();
                betDays= DateUtil.daysBetween(indDate,endDate);
            }

        }

        return R.ok().put("lookDate", 0);
    }

}
